﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.CodeDom.Compiler;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Markup;

    [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
    public class BusinessLogicClassDialog : Window, IComponentConnector, IStyleConnector
    {
        private bool _contentLoaded;
        internal TextBlock AvailableContextLabel;
        internal Button CancelButton;
        internal ComboBox ContextComboBox;
        internal TextBlock DomainServiceClassNameLabel;
        internal TextBox DomainServiceClassNameTextBox;
        internal CheckBox EnableClientAccessCheckbox;
        internal CheckBox EnableODataEndpointCheckbox;
        internal ListView EntityListView;
        internal CheckBox GenerateMetadataCheckbox;
        internal Button OKButton;

        public BusinessLogicClassDialog()
        {
            this.InitializeComponent();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            base.DialogResult = false;
        }

        private void CheckBox_ClassMetadata_EnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            CheckBox box = sender as CheckBox;
            if (box != null)
            {
                if (box.IsEnabled)
                {
                    BusinessLogicViewModel dataContext = box.DataContext as BusinessLogicViewModel;
                    if (dataContext != null)
                    {
                        box.IsChecked = new bool?(dataContext.IsMetadataClassGenerationAllowed);
                    }
                }
                else
                {
                    box.IsChecked = false;
                }
            }
        }

        private void CheckBox_Clicked(object sender, RoutedEventArgs e)
        {
            IList selectedItems = this.EntityListView.SelectedItems;
            CheckBox box = sender as CheckBox;
            if (box != null)
            {
                if ((selectedItems.Count == 0) || !selectedItems.Contains(box.DataContext))
                {
                    this.EntityListView.SelectedItem = box.DataContext;
                }
                bool flag = box.IsChecked.HasValue ? box.IsChecked.Value : false;
                foreach (object obj2 in selectedItems)
                {
                    string str;
                    EntityViewModel model = obj2 as EntityViewModel;
                    if ((model == null) || ((str = box.Name) == null))
                    {
                        continue;
                    }
                    if (!(str == "IncludeEntityCheckbox"))
                    {
                        if (str == "EnableEditingCheckbox")
                        {
                            goto Label_00B6;
                        }
                        continue;
                    }
                    model.IsIncluded = flag;
                    if (flag)
                    {
                        continue;
                    }
                Label_00B6:
                    model.IsEditable = flag;
                }
            }
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Model.DisplayHelp();
        }

        private void ContextComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedIndex = this.ContextComboBox.SelectedIndex;
            if ((selectedIndex >= 0) && (selectedIndex < this.Model.ContextViewModels.Count))
            {
                this.Model.CurrentContextViewModel = this.Model.ContextViewModels[selectedIndex];
            }
        }

        private void EntityListView_Loaded(object sender, RoutedEventArgs e)
        {
            GridView view = this.EntityListView.View as GridView;
            double actualWidth = view.Columns[1].ActualWidth;
            double num2 = this.EntityListView.ActualWidth;
            actualWidth = Math.Min(actualWidth, num2 / 2.0);
            view.Columns[0].Width = num2 - actualWidth;
            view.Columns[1].Width = actualWidth;
        }

        [DebuggerNonUserCode]
        public void InitializeComponent()
        {
            if (!this._contentLoaded)
            {
                this._contentLoaded = true;
                Uri resourceLocator = new Uri("/Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.10.0;component/domainservicewizard/businesslogicclassdialog.xaml", UriKind.Relative);
                Application.LoadComponent(this, resourceLocator);
            }
        }

        private void InitializeFromModel()
        {
            int count = this.Model.ContextViewModels.Count;
            if (count > 0)
            {
                int num2 = (count > 1) ? 1 : 0;
                this.Model.CurrentContextViewModel = this.Model.ContextViewModels[num2];
                this.ContextComboBox.SelectedIndex = num2;
            }
        }

        private void InitializePresentation()
        {
            GridView view = this.EntityListView.View as GridView;
            if (((view != null) && (view.Columns != null)) && (view.Columns.Count == 2))
            {
                this.EntityListView.Loaded += new RoutedEventHandler(this.EntityListView_Loaded);
            }
            this.GenerateMetadataCheckbox.ToolTip = string.Format(CultureInfo.CurrentCulture, "BusinessLogicClass_Metadata_Tooltip", new object[] { this.Model.ClassName });
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            base.DialogResult = true;
        }

        [EditorBrowsable(EditorBrowsableState.Never), DebuggerNonUserCode]
        void IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
                case 1:
                    ((CommandBinding) target).Executed += new ExecutedRoutedEventHandler(this.CommandBinding_Executed);
                    return;

                case 2:
                    this.DomainServiceClassNameLabel = (TextBlock) target;
                    return;

                case 3:
                    this.DomainServiceClassNameTextBox = (TextBox) target;
                    return;

                case 4:
                    this.EnableClientAccessCheckbox = (CheckBox) target;
                    return;

                case 5:
                    this.EnableODataEndpointCheckbox = (CheckBox) target;
                    return;

                case 6:
                    this.AvailableContextLabel = (TextBlock) target;
                    return;

                case 7:
                    this.ContextComboBox = (ComboBox) target;
                    this.ContextComboBox.SelectionChanged += new SelectionChangedEventHandler(this.ContextComboBox_SelectionChanged);
                    return;

                case 8:
                    this.EntityListView = (ListView) target;
                    return;

                case 11:
                    this.GenerateMetadataCheckbox = (CheckBox) target;
                    this.GenerateMetadataCheckbox.IsEnabledChanged += new DependencyPropertyChangedEventHandler(this.CheckBox_ClassMetadata_EnabledChanged);
                    return;

                case 12:
                    this.OKButton = (Button) target;
                    this.OKButton.Click += new RoutedEventHandler(this.OKButton_Click);
                    return;

                case 13:
                    this.CancelButton = (Button) target;
                    this.CancelButton.Click += new RoutedEventHandler(this.CancelButton_Click);
                    return;
            }
            this._contentLoaded = true;
        }

        [DebuggerNonUserCode, EditorBrowsable(EditorBrowsableState.Never)]
        void IStyleConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
                case 9:
                    ((CheckBox) target).Click += new RoutedEventHandler(this.CheckBox_Clicked);
                    return;

                case 10:
                    ((CheckBox) target).Click += new RoutedEventHandler(this.CheckBox_Clicked);
                    return;
            }
        }

        internal BusinessLogicViewModel Model
        {
            get
            {
                return (base.DataContext as BusinessLogicViewModel);
            }
            set
            {
                if (base.DataContext != value)
                {
                    base.DataContext = value;
                    if (base.DataContext != null)
                    {
                        this.InitializePresentation();
                        this.InitializeFromModel();
                    }
                }
            }
        }
    }
}

